const mysql = require('mysql2/promise');
const bcrypt = require('bcrypt');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456', // 使用您的实际密码
  database: 'chatime',
  port: 3306
};

async function createTestUsers() {
  const pool = mysql.createPool(dbConfig);
  
  try {
    console.log('🔗 连接数据库...');
    await pool.getConnection();
    console.log('✅ 数据库连接成功');
    
    // 生成测试用户数据
    const testUsers = [
      {
        id: 'user1',
        phone: '13800138001',
        password: '123456', // 明文密码
        name: '李文静',
        avatar: 'avatar1',
        signature: '茶香袅袅，岁月静好',
        following: 156,
        followers: 234,
        dynamics: 67
      },
      {
        id: 'user2',
        phone: '13800138002',
        password: '123456', // 明文密码
        name: '王明远',
        avatar: 'avatar2',
        signature: '紫砂壶泡普洱，人生一大乐事',
        following: 89,
        followers: 156,
        dynamics: 34
      },
      {
        id: 'user3',
        phone: '13800138003',
        password: '123456', // 明文密码
        name: '陈雨桐',
        avatar: 'avatar3',
        signature: '茶道即人道，品茶品人生',
        following: 203,
        followers: 445,
        dynamics: 123
      }
    ];
    
    console.log('🔐 生成密码哈希...');
    
    // 为每个用户生成密码哈希
    for (const user of testUsers) {
      const hashedPassword = await bcrypt.hash(user.password, 10);
      user.password = hashedPassword;
    }
    
    console.log('🗑️ 清空现有用户数据...');
    await pool.execute('DELETE FROM users WHERE id IN (?, ?, ?)', ['user1', 'user2', 'user3']);
    
    console.log('📝 插入测试用户...');
    
    // 插入测试用户
    for (const user of testUsers) {
      await pool.execute(`
        INSERT INTO users (id, phone, password, name, avatar, signature, following, followers, dynamics, create_time, update_time) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
      `, [
        user.id,
        user.phone,
        user.password,
        user.name,
        user.avatar,
        user.signature,
        user.following,
        user.followers,
        user.dynamics
      ]);
      
      console.log(`✅ 创建用户: ${user.name} (${user.phone})`);
    }
    
    console.log('\n🎉 测试用户创建完成！');
    console.log('\n📱 登录信息：');
    console.log('手机号: 13800138001, 密码: 123456');
    console.log('手机号: 13800138002, 密码: 123456');
    console.log('手机号: 13800138003, 密码: 123456');
    
  } catch (error) {
    console.error('❌ 创建测试用户失败:', error);
  } finally {
    await pool.end();
  }
}

// 运行脚本
createTestUsers(); 